<?php
class GoodsPlugin extends BasePlugin {
	private $_num = 5; //显示历史购买记录条数
	

	//显示兑换数量和兑换名单
	public function api() {
		
		if ($_POST ['goodsid'] == 0) {
			$history = GoodsModel::instance ()->getLastProuct ( 6 );
			$member = new MemberService ();
			foreach ( $history as $k => $v ) {
				$user = $member->getMemberByUid ( $v ['uid'] );
				$history [$k] ['face'] = $user['face'];
			}
			
		} else {
			$rs = GoodsModel::instance ()->getProductNum ( $_POST ['goodsid'] );
			$history = GoodsModel::instance ()->getProductHisotry ( $_POST ['goodsid'], $this->_num );
		}
		$rs ['history'] = $history;
		$this->sendNotice ( self::SUCCESS, $rs );
	}
	
	public function run() {
	}
	
	public function v() {
	}
}

class GoodsModel extends OrderModel {
	
	public function getProductNum($goodsid) {
		$sql = "select sum(num) num from $this->_order_info where goods_id=$goodsid";
		return $this->fetch ( $sql );
	
	}
	
	public function getProductHisotry($goodsid, $limit) {
		$sql = "select a.num,b.shr,FROM_UNIXTIME(b.addtime,\"%Y-%m-%d %H:%i\") addtime from w_order_info a left join w_order_list b on a.orderno=b.orderno where  goods_id=$goodsid ORDER BY a.id DESC LIMIT $limit";
		return $this->fetchAll ( $sql );
	}
	
	public function getLastProuct($limit) {
		$sql = "select a.num,b.uid,b.shr,a.goods_name,FROM_UNIXTIME(b.addtime,\"%m-%d %H:%i\") addtime from w_order_info a left join w_order_list b on a.orderno=b.orderno group by b.shr ORDER BY a.id DESC  LIMIT $limit";
		return $this->fetchAll ( $sql );
	}
	
	/**
	 * 
	 * @return GoodsModel
	 */
	public static function instance() {
		return parent::_instance ( __CLASS__ );
	}
}